﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Common;
using H2.NET.Web.Workflow.DwzCode;
using Ultimus.OC;
using Ultimus.WFServer;
using UServLib;
using H2.NET.Business.Core;
using H2.NET.Data.Common;

namespace H2.NET.Web.Workflow.Workflow
{
    public partial class TaskVariables : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var taskId = AksRequest.GetString("taskid").Trim();
            var task = new Task();
            task.InitializeFromTaskId(taskId);

            Variable[] variables;
            var strError = "";
            task.GetAllTaskVariables(out variables, out strError);

            if (variables != null && strError == "")
            {
                var dtVariables = new DataTable();
                dtVariables.Columns.Add("variableName");
                dtVariables.Columns.Add("variableValue");
                foreach (var variable in variables)
                {
                    var row = dtVariables.NewRow();
                    row["variableName"] = variable.strVariableName;
                    if (variable.objVariableValue != null)
                    {
                        if (variable.objVariableValue.Length == 1)
                        {
                            row["variableValue"] = "<input style='width:99%;' name='" + variable.strVariableName + "' value='" +
                                                   variable.objVariableValue[0] + "' type='text' />";

                        }
                        else if (variable.objVariableValue.Length > 1)
                        {
                            var sbValues = new StringBuilder();
                            sbValues.Append("<input type='text' style='width:99%;' name='" + variable.strVariableName + "' value='");
                            for (var i = 0; i < variable.objVariableValue.Length; i++)
                            {
                                if (variable.objVariableValue[i] == null)
                                {
                                    variable.objVariableValue[i] = "";
                                }

                                sbValues.Append(variable.objVariableValue[i] + " ");
                            }
                            sbValues.Append("' />");
                            row["variableValue"] = sbValues.ToString();

                        }
                    }
                    else
                    {
                        row["variableValue"] = "<input style='width:99%;' name='" + variable.strVariableName + "' value='' type='text' />";
                    }

                    dtVariables.Rows.Add(row);
                }

                gvVariables.DataSource = dtVariables;
                gvVariables.DataBind();
            }
        }
    }
}